import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ClientTest {
    private final static int clientCount = 2;
    private final static int index = 1;
    private final static TCPClient[] tcpClients = new TCPClient[clientCount];

    public static void main(String[] args) {
        CreateIni();
    }

    private static void CreateIni() {
        String clientMessage = "clientMessage";
        for (int i = 0; i < clientCount; i++) {
            TCPClient tcpClient = new TCPClient("192.168.3.22", 60000);
            tcpClients[i] = tcpClient;
            try {
                tcpClient.start();
                Thread.sleep(200);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        SendDate(clientMessage);
    }

    private static void SendDate(String message) {
        System.out.println("客户端准备就绪，可以开始发送命令。。。");
        boolean stopSign = false;
        InputStream in = System.in;
        BufferedReader input = new BufferedReader(new InputStreamReader(in));
        do {
// 键盘读取一行
            String str = null;
            try {
                System.out.println("再次发送命令。。。");
                str = input.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (str.equals("exit")) {
                stopSign = true;
            }

            if (str.length() == 0) {
                continue;
            }
            if (!stopSign) {
                for (int j = 0; j < index; j++) {
                    for (int i = 0; i < tcpClients.length; i++) {
                        try {
                            tcpClients[i].write(message + "--" + ((j + 1) * index + i));
                            Thread.sleep(200);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } while (!stopSign);

        for (int i = 0; i < tcpClients.length; i++) {
            tcpClients[i].Close();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
